﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Tool.hbm2ddl;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Automapping;

namespace WhiteFox.DAlessio.Patterns.Repository
{
    public abstract class Repository<T> : NHibernateBase, IRepository<T>
    {
        public IList<T> GetAll()
        {
            IList<T> list;
            
            using (ISession session = GetNHBSession())
            {
                session.BeginTransaction();
                list = session.CreateCriteria(typeof(T)).List<T>();
                session.Transaction.Commit();

            }

            return list;

        }
        public T GetByID(int id)
        {
            T list;
            
            using (ISession session = GetNHBSession())
            {
                session.BeginTransaction();
                list = session.Get<T>(id);
                session.Transaction.Commit();

            }

            return list;


        }
        public void Save(T obj)
        {
            
            using (ISession session = GetNHBSession())
            {
                session.BeginTransaction();
                session.Save(obj);
                session.Transaction.Commit();

            }

        }
        public void Delete(T obj)
        {
            
            using (ISession session = GetNHBSession())
            {
                session.BeginTransaction();
                session.Delete(obj);
                session.Transaction.Commit();

            }
        }
        public void SaveUpdate(T obj)
        {
            
            using (ISession session = GetNHBSession())
            {
                session.BeginTransaction();
                session.SaveOrUpdate(obj);
                session.Transaction.Commit();

            }

        }

        public void DeleteByID(string idPropertyName, int objId)
        {
            
            using (ISession session = GetNHBSession())
            {
                session.BeginTransaction();
                var result = session.CreateCriteria(typeof(T))
                                     .Add(Restrictions.Eq(idPropertyName, objId))
                                     .List<T>();
                if (result.Count > 0)
                {
                    session.Delete(result[0]);
                }
                session.Transaction.Commit();
            }

        }

        public IList<T> GetByPropertyName(string idPropertyName, string pvalue)
        {


            IList<T> list;
            
            using (ISession session = GetNHBSession())
            {
                session.BeginTransaction();
                list = session.CreateCriteria(typeof(T))
                                      .Add(Restrictions.Like(idPropertyName, pvalue,MatchMode.Anywhere))
                                      .List<T>();

                session.Transaction.Commit();
            }
            return list;

        }

    }
}
